//
//  PopoverViewController.swift
//  TTProduct-macOS
//
//  Created by Toj on 5/8/21.
//

import Cocoa

class PopoverViewController: TTViewController {
    
    lazy var backgroundImageStyle: TTImageStyle = {
        let style = TTImageStyle()
        style.normal = NSImage(named: "")
        style.press = NSImage(named: "")
        style.hover = NSImage(named: "")
        return style
    }()
    
//    lazy var titleColorStyle: TTColorStyle = {
//        let style = TTColorStyle()
//        style.normal = NSColor
//        style.press = NSImage(named: "")
//        return style
//    }()
    
    lazy var items: [TTPopoverViewController.TTItem] = {
        var items: [TTPopoverViewController.TTItem] = []
        
        let item = TTPopoverViewController.TTItem()
        item.title = "设备检测"
        item.image = NSImage(named: "homeEnumDeviceTesting")
        //let itemImageStyle = TTImageStyle()
        //item.imageStyle = itemImageStyle
        
        let item2 = TTPopoverViewController.TTItem()
        item2.title = "上传日志"
        item2.image = NSImage(named: "homeEnumLogUp")
        
        let item3 = TTPopoverViewController.TTItem()
        item3.title = "学习插件"
        item3.image = NSImage(named: "homeEnumPlugin")
        
        let item4 = TTPopoverViewController.TTItem()
        item4.title = "注销退出"
        item4.image = NSImage(named: "homeEnumSignOut")
        
        let item5 = TTPopoverViewController.TTItem()
        item5.title = "测试房间"
        item5.image = NSImage(named: "homeEnumTestroom")
        
        let item6 = TTPopoverViewController.TTItem()
        item6.title = "软件升级"
        item6.image = NSImage(named: "homeEnumUpgrade")
        
        items.append(item)
        items.append(item2)
        items.append(item3)
        items.append(item4)
        items.append(item5)
        items.append(item6)
        return items
    }()
    
    lazy var appearance: TTPopoverViewController.TTAppearance = {
        let appearance = TTPopoverViewController.TTAppearance()
        appearance.backgroundColor = .clear
        appearance.itemHeight = 24
        appearance.itemWidth = 84
        appearance.spacing = 6
        appearance.cornerRadius = 4
        appearance.titleFont = NSFont.systemFont(ofSize: 14)
        appearance.titleAlignment = .right
        appearance.titleColor = NSColor(hexColor: "#A1B6C5")
        appearance.backgroundImageStyle = backgroundImageStyle
        return appearance
    }()
    
    weak var popover: TTPopoverViewController? {
        let popover = TTPopoverViewController()
        popover.delegate = self
        popover.appearance = appearance
        popover.backgroundColor = .white
        popover.bottomInset = 10
        popover.itemLineSpacing = 10
        popover.contentSize = CGSize(width: 104, height: 100)
        popover.items = items
        return popover
    }
    
    @objc private func didTapButton(button: NSButton) {
       
        popover?.showRelative(of: button, preferredEdge: .maxY)
    }
}

extension PopoverViewController: TTPopoverViewControllerDelegate {
    func popover(viewController: TTPopoverViewController, didTapRowAt item: TTPopoverViewController.TTItem) {
        print(item.index)
    }
}
